import store from "../store";
import Md5 from 'js-md5';
import txtIcon from '../assets/images/tree-topLevel.png';
import acntIcon from '../assets/images/folderIcon.png';

/**
 *判断身份证号码
 */
export const isIdCard = (rule, value, callback) => {
  if (value) {
    let regName = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    let aCity = {
      11: '北京',
      12: '天津',
      13: '河北',
      14: '山西',
      15: '内蒙古',
      21: '辽宁',
      22: '吉林',
      23: '黑龙江',
      31: '上海',
      32: '江苏',
      33: '浙江',
      34: '安徽',
      35: '福建',
      36: '江西',
      37: '山东',
      41: '河南',
      42: '湖北',
      43: '湖南',
      44: '广东',
      45: '广西',
      46: '海南',
      50: '重庆',
      51: '四川',
      52: '贵州',
      53: '云南',
      54: '西藏',
      61: '陕西',
      62: '甘肃',
      63: '青海',
      64: '宁夏',
      65: '新疆',
      71: '台湾',
      81: '香港',
      82: '澳门',
      91: '国外'
    };
    let iSum = 0;
    if (!regName.test(value))
      return callback(new Error('你输入的身份证长度或格式错误'));
    value = value.replace(/x$/i, 'a');
    if (aCity[parseInt(value.substr(0, 2))] == null)
      return callback(new Error('你的身份证地区非法'));
    let sBirthday =
      value.substr(6, 4) +
      '-' +
      Number(value.substr(10, 2)) +
      '-' +
      Number(value.substr(12, 2));
    var d = new Date(sBirthday.replace(/-/g, '/'));
    if (
      sBirthday !=
      d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()
    )
      return callback(new Error('身份证上的出生日期非法'));
    for (var i = 17; i >= 0; i--)
      iSum += Math.pow(2, i) % 11 * parseInt(value.charAt(17 - i), 11);
    if (iSum % 11 != 1) return callback(new Error('你输入的身份证号非法'));
    callback();
    // let regName = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
    // if (regName.test(value)) {
    //   callback()
    // } else {
    //   return callback(new Error('请输入合法的身份证号'))
    // }
  } else {
    return callback();
  }
};

/**
 *判断手机号格式
 */
export const isPhone = (rule, value, callback) => {
  if (value) {
    let isMob = /^1[3,4,5,6,7,8,9]\d{9}$/;
    if (isMob.test(value) || value.length == 11) {
      callback();
    } else {
      return callback(new Error('请输入合法的手机号'));
    }
  } else {
    return callback();
  }
};
export const isPhone1 = (rule, value, callback) => {
  if (value) {
    if (value.length == 11) {
      callback();
    } else {
      return callback(new Error('请输入合法的电话'));
    }
  } else {
    return callback();
  }
};

/**
 *判断是否为数字
 */
export const isNumber = (rule, value, callback) => {
  if (value) {
    let regNum = /^[0-9]+.?[0-9]*$/;
    if (regNum.test(value)) {
      callback();
    } else {
      return callback(new Error('请输入合法的数字'));
    }
  } else {
    return callback();
  }
};

/**
 *判断是否为邮箱
 */
export const isEmail = (rule, value, callback) => {
  if (value) {
    // eslint-disable-next-line no-useless-escape
    let regEmail = /^[A-Za-z0-9]+([_\.][A-Za-z0-9]+)*@([A-Za-z0-9\-]+\.)+[A-Za-z]{2,6}$/;
    if (regEmail.test(value)) {
      callback();
    } else {
      return callback(new Error('请输入正确的邮箱'));
    }
  } else {
    return callback();
  }
};

/**
 * 禁止输入汉字
 */
export const isCheckData = (rule, value, callback) => {
  if (value) {
    if (/[\u4E00-\u9FA5]/g.test(value)) {
      callback(new Error('不能输入汉字!'));
    } else {
      callback();
    }
  }
  callback();
};

/**
 * 签名
 */
export const settingSign = type => {
  let timestamp = Date.parse(new Date()) + '';
  let { appKey, appSecret } = store.state.common.userInfo;
  appSecret = appSecret.substring(4); // 去前4后5
  appSecret = appSecret.substring(0, appSecret.length - 5);
  let sign = Md5(
    'client_id' +
    appKey +
    'client_secret' +
    appSecret +
    '_timestamp' +
    timestamp
  );
  let data = {
    timestamp,
    sign
  };
  return data;
};

/**
 * 循环tree数据给每个节点加上图标  //  sonCategories
 * @param {data} data/树状结构数据
 */
export const mapTreesonCategories = data => {
  data.map(item => {
    if (item.children && item.children.length !== 0) {
      item.icons = acntIcon;
      mapTreesonCategories(item.children);
    } else {
      item.icons = txtIcon;
    }
  });
};

/**
 *判断银行卡号
 */
export const isBank = (rule, value, callback) => {
  if (value) {
    let bank = /^([1-9]{1})(\d{15}|\d{16}|\d{17}|\d{18})$/;
    if (bank.test(value)) {
      callback();
    } else {
      return callback(new Error('请输入正确的银行卡号'));
    }
  } else {
    return callback();
  }
};

// 防抖
export const _debounce = (fn, delay = 300) =>{
  var timer = null;
  return function () {
    var _this = this;
    var args = arguments;
    if (timer) clearTimeout(timer); 
    timer = setTimeout(function () {
      fn.apply(_this, args);
    }, delay);
  };
};
